package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Assert;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.fastjson.JSON;
import com.sun.prism.image.ViewPort;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public boolean checkPhone(String phone) {
        //如果结果为null,可以注册,返回true
        return userInfoMapper.checkPhone(phone) == null;
    }


    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;
    @Override
    public AjaxResult userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        AjaxResult result = new AjaxResult();

        try {
            //校验数据
            //非空校验
            Assert.hasLength(verifyCode,"验证码不能为空!");
            Assert.hasLength(phone,"手机号不能为空!");
            Assert.hasLength(nickname,"昵称不能为空!");
            Assert.hasLength(password,"密码不能为空!");
            Assert.hasLength(rpassword,"确认密码不能为空!");
            //一致性校验
            //验证码校验
            Assert.equalsIgnoreCase(verifyCode,verifyCodeCacheService.getVerifyCode(phone),"验证码错误!");
            //密码确认校验
            Assert.equalsIgnoreCase(password,rpassword,"两次密码不一致!");
            //将注册信息存入用户对象
            UserInfo user = new UserInfo();
            user.setPhone(phone);
            user.setNickname(nickname);
            user.setPassword(password);
            user.setCity("云梦洲");
            user.setEmail("biubiubiu@163.com");
            user.setInfo("翻过这座山");
            user.setHeadImgUrl("/images/default.png");

            //保存用户信息
            userInfoMapper.insert(user);

        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @Reference
    private IUserInfoCacheService userInfoCacheService;
    @Override
    public AjaxResult userLogin(String phone,String password) {
        AjaxResult result = new AjaxResult();
        UserInfo user = userInfoMapper.userLogin(phone, password);
        if (user == null) {
          return result.mark("账号或密码错误");
        }
        //登录成功,生成一个token
        String token = UUID.randomUUID().toString().replace("-", "");
        //缓存用户对象
        userInfoCacheService.setUserInfo(token, JSON.toJSONString(user));
        //将token传递给控制器
        result.setData(token);
        return result;
    }

    @Override
    public AjaxResult updateBaseInfo(UserInfo user) {
        AjaxResult result = new AjaxResult();
        try {
            userInfoMapper.updateBaseInfo(user);
            //查询被修改的对象将其重新存入缓存中
            user = userInfoMapper.selectById(user.getId());
            //修改成功,生成一个token
            String token = UUID.randomUUID().toString().replace("-", "");
            //缓存用户对象
            userInfoCacheService.setUserInfo(token, JSON.toJSONString(user));
            //将token传递给控制器
            result.setData(token);
        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }

    public UserInfo getById(Long userId) {

        return userInfoMapper.selectById(userId);
    }

    @Override
    public AjaxResult updatePassword(UserInfo user,String password, String rpassword, String smscode) {
        AjaxResult result = new AjaxResult();

        try {
            //校验数据
            //非空校验
            Assert.hasLength(smscode,"验证码不能为空!");
            Assert.hasLength(password,"密码不能为空!");
            Assert.hasLength(rpassword,"确认密码不能为空!");
            //一致性校验
            //验证码校验
            Assert.equalsIgnoreCase(smscode,verifyCodeCacheService.getVerifyCode(user.getPhone()),"验证码错误!");
            //密码确认校验
            Assert.equalsIgnoreCase(password,rpassword,"两次密码不一致!");
            //将注册信息存入用户对象
            user.setPassword(password);
            //修改用户信息
            userInfoMapper.updatePassword(user);

        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    public AjaxResult updateHeadImg(UserInfo user) {
        AjaxResult result = new AjaxResult();
        try {
            userInfoMapper.updateHandImg(user);

        } catch (Exception e) {
            e.printStackTrace();
            return result.mark("修改失败");
        }
        return result;
    }

    public void updatePhone(UserInfo user) {
        userInfoMapper.updatePhone(user);
    }

    public List<UserInfo> selectAll() {
        return userInfoMapper.selectAll();
    }

    public List<String> selectAllPhone() {
        return userInfoMapper.selectAllPhone();
    }

    public UserInfo selectByPhone(String phone) {
        return userInfoMapper.selectByPhone(phone);
    }


}
